package com.qhp.androidsocket;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class OrderActivity extends AppCompatActivity {
    Socket socket=null;
    TextView tv_ljzt;
    EditText et_num;
    Button btn_lj;
    PrintStream output;
    public static String ip="192.168.1.10";
    public static int finalDkh=1000;
    String s;
    String ss = new String("a");
    String input1;
    List list=new ArrayList();
    int q=1;
    private RoundProgressBar mRoundProgressBar2 ;
    private int progress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        tv_ljzt = (TextView) findViewById(R.id.tv_ljzt);
        et_num = (EditText) findViewById(R.id.et_num);
        btn_lj = (Button) findViewById(R.id.btn_lj);
        mRoundProgressBar2 = (RoundProgressBar) findViewById(R.id.roundProgressBar2);
        et_num.post(new Runnable() {
            @Override
            public void run() {
                et_num.setText("");
                et_num.setError("请输入数量！");
            }
        });
        new Thread() {
            @Override
            public void run() {
                try {
                    socket = new Socket(ip, finalDkh);
                } catch (IOException e) {
                    socket = null;
                }
                if (socket != null) {
                    tv_ljzt.setText("连接成功");
                    tv_ljzt.setTextColor(Color.GREEN);
                    try {
                        output = new PrintStream(socket.getOutputStream());
                        new Thread() {
                            public void run() {
                                if (q == 1) {
                                    output.print("01");
                                    q++;
                                }
                            }
                        }.start();
                        for (; ; ) {
                            InputStream inputStream = socket.getInputStream();
                            byte[] bytes1 = new byte[1024];
                            int i = inputStream.read(bytes1);
                            s = new String(bytes1, 0, i);
                            System.out.print(s);
                            if (ss.equals(s.trim())) {
                                et_num.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        et_num.setError("数量不足");
                                    }
                                });
                            }else{
                                TimerTask task = new TimerTask() {
                                    public void run() {
                                        String[] strings = s.trim().split("/");
                                        for (int i = 0; i < strings.length; i++) {
                                            list.add(strings[i]);
                                        }
                                    }
                                };
                                Timer timer = new Timer();
                                timer.schedule(task, 1000);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    tv_ljzt.post(new Runnable() {
                        @Override
                        public void run() {
                            tv_ljzt.setText("连接失败");
                            tv_ljzt.setTextColor(Color.RED);
                        }
                    });
                }
            }
        }.start();
        btn_lj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(OrderActivity.this, MainActivity.class);
                MainActivity mainActivity = new MainActivity();
                mainActivity.panduan = 2;
                OrderActivity.this.startActivity(intent);
            }
        });
        list.add("请选择！");
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(OrderActivity.this, android.R.layout.simple_spinner_item, list);
                adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
                Spinner sp = (Spinner) findViewById(R.id.sp_name);
                sp.setAdapter(adapter);
                sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    // parent： 为控件Spinner   view：显示文字的TextView   position：下拉选项的位置从0开始
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        //获取Spinner控件的适配器
                        ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent.getAdapter();
                        input1 = adapter.getItem(position);
                        System.out.println(input1);
                        et_num.post(new Runnable() {
                            @Override
                            public void run() {
                                et_num.setText("");
                                et_num.setError("请输入数量！");
                            }
                        });
                    }

                    //没有选中时的处理
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
            }
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btn_order:
               final StringBuilder sb = new StringBuilder("02");
                if (socket==null){
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            while(progress <= 100){
                                progress += 3;

                                System.out.println(progress);
                                mRoundProgressBar2.setProgress(progress);

                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(OrderActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                                    progress=0;
                                    mRoundProgressBar2.setProgress(progress);
                                }
                            });
                        }
                    }).start();
                    //Toast.makeText(this,"请连接服务器", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    new Thread() {
                        @Override
                        public void run() {
                            String a = input1;
                            String b=et_num.getText().toString();
                            sb.append(a);
                            sb.append("/");
                            sb.append(b);
                            if (a=="选择！"||a.equals("选择！")||b.equals("0")||b.equals(""))
                            {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(OrderActivity.this, "请选择并输入正确的数", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }else {
                                    output.print(sb);
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(OrderActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                            }
                        }
                    }.start();
                }
                break;
        }
    }
}
